<template>
  <div class="organizationalStructure">
    <el-row :gutter="20" type="flex" justify="space-between">
      <el-col :span="4">
        <el-card>
          <el-input
            placeholder="搜索地区/工厂/部门"
            prefix-icon="el-icon-search"
            v-model="searchName"
          >
          </el-input>
          <el-tree
            class="filter-tree"
            :data="treeData"
            :prop="defaultProps"
            default-expand-all
            :filter-node-method="filterNode"
            ref="tree"
          >
          </el-tree>
        </el-card>
      </el-col>
      <el-col :span="20">
        <el-card>
          <el-tabs v-model="activeMenu">
            <el-tab-pane label="员工(20)" name="1">
              <my-table
                :columns="columns"
                :data="data"
                :showSelection="true"
                @selectChange="selectChange"
                v-if="activeMenu == 1"
              >
                <template slot="table-title">
                  <el-button
                    type="primary"
                    @click="changePasswordVisible = true"
                    >修改密码</el-button
                  >
                  <el-button type="danger">停用员工</el-button>
                  <el-button type="primary">启用员工</el-button>
                  <el-button type="primary" @click="modifyRoleVisible = true"
                    >修改角色</el-button
                  >
                </template>
                <template v-slot:action="{ row }">
                  <el-button
                    type="text"
                    @click="$router.push({ name: 'editEmployee' })"
                    >编辑</el-button
                  >
                  <el-button type="text">修改密码</el-button>
                  <el-button type="text" :class="row.status ? '' : 'danger'">{{
                    row.status ? "启用" : "停用"
                  }}</el-button>
                </template>
              </my-table>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      title="修改密码"
      width="50%"
      :visible.sync="changePasswordVisible"
    >
      <el-form :model="passwordForm" ref="passwordFormRef" label-width="auto">
        <el-form-item label="输入新密码">
          <el-input
            v-model="passwordForm.password"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose(0)">取 消</el-button>
        <el-button type="primary" @click="onOk(0)">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改角色" width="50%" :visible.sync="modifyRoleVisible">
      <div>{{ role }} 修改为</div>
      <el-form
        :model="modifyRoleForm"
        ref="modifyRoleFormRef"
        label-width="auto"
      >
        <el-form-item label="角色选择">
          <el-select v-model="modifyRoleForm.role" placeholder="请选择">
            <el-option label="" value=""></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose(1)">取 消</el-button>
        <el-button type="primary" @click="onOk(1)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import myTable from "@/components/table/myTable.vue";
export default {
  components: { myTable },
  data() {
    return {
      searchName: "",
      treeData: [
        {
          id: 1,
          label: "一级 1",
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      activeMenu: "1",
      columns: [
        {
          label: "员工",
          prop: "name",
        },
        {
          label: "账号",
          prop: "addTime",
        },
        {
          label: "所属部门",
          prop: "department",
        },
        {
          label: "职位",
          prop: "factory",
        },
        {
          label: "添加时间",
          prop: "factory",
        },
        {
          label: "角色",
          prop: "factory",
        },
        {
          label: "联系电话",
          prop: "factory",
        },
        {
          label: "操作",
          prop: "action",
          slot: "action",
          minwidth: "200",
        },
      ],
      data: [],
      selectChangeList: [],
      changePasswordVisible: false,
      passwordForm: {},
      modifyRoleVisible: false,
      modifyRoleForm: {},
      role: "",
    };
  },
  created() {},
  mounted() {},
  watch: {
    searchName(val) {
      this.$refs.tree.filter(val);
    },
  },

  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    selectChange(val) {
      this.selectChangeList = val;
    },
    selectChange(val) {
      this.selectChange = val;
    },
    onClose(val) {
      if (val == 0) {
        this.changePasswordVisible = false;
      } else if (val == 1) {
        this.modifyRoleVisible = false;
      }
    },
    onOk(val) {
      if (val == 0) {
        this.changePasswordVisible = false;
      } else if (val == 1) {
        this.modifyRoleVisible = false;
        const role = this.selectChangeList.map((i) => {
          return i.name;
        });
        this.role = role.join("、");
      }
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/styles/element-variables.scss";
.organizationalStructure {
  width: 100%;
}
.el-card {
  height: 100%;
  position: relative;
}
.add {
  position: absolute;
  bottom: 5%;
  left: 0;
  padding: 0 20px;
  width: 100%;
  ::v-deep .el-button {
    width: 100%;
  }
}
.card-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  h4 {
    flex: 1;
    margin: unset;
  }
  ::v-deep .input-with-select {
    width: unset;
  }
}
::v-deep .el-tree-node.is-current.is-focusable {
  background: #e8f4ff !important;
  color: $--color-primary;
}
.danger {
  color: $--color-primary;
}
</style>
